package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.NewsComment;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.NewsCommentQueryObject;
import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.INewsCommentService;
import cn.wolfcode.trip.base.service.INewsService;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.alibaba.fastjson.JSONReader;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/newsPages")
public class NewsController {
    /*
     * url:news
     * 请求方式:get
     * 返回结果:pageInfo
     * */
    @Autowired
    private INewsService newsService;
    @Autowired
    private INewsCommentService newsCommentService;

    @GetMapping
    public PageInfo query(NewsQueryObject qo) {
        // 已发布的，已发布的就是已公开的
        qo.setState(News.STATE_PUBLIC);
        return newsService.query(qo);
    }

    //获取某一篇日报
    @GetMapping("{id}")
    public News selectById(@PathVariable Long id) {
        return newsService.selectByPrimaryKey(id);
    }

    //更新浏览次数
    @PutMapping({"{id}/browseNum"})
    public JsonResult updateBroseNumById(@PathVariable Long id, Integer browseNum) {
        newsService.updateBroseNumById(id, browseNum);
        return new JsonResult();
    }

    //更新点赞次数
    @PutMapping({"{id}/likeNum"})
    public JsonResult updateLikeNumById(@PathVariable Long id, Integer likeNum) {
        newsService.updateLikeNumById(id, likeNum);
        return new JsonResult();
    }

    //获取日报评论
    @GetMapping("{newsId}/comments")
    public PageInfo selectCommentsByNewsId(NewsCommentQueryObject qo) {
        return newsCommentService.query(qo);
    }

    //保存日报评论
    @PostMapping("{news.id}/comments")
    public JsonResult saveNewsComment(NewsComment newsComment) {
        newsCommentService.saveNewsComment(newsComment);
        return new JsonResult();
    }


    //查询该文章的是否有这名收藏者?
    @GetMapping("{newsId}/collections/{userId}")
    public JsonResult selectNewsByUserId(@PathVariable Long userId,@PathVariable Long newsId) {
        News news = newsService.selectNewsByUserId(userId,newsId);
        JsonResult result = new JsonResult();
        if (news != null) {
            return new JsonResult();
        }
        result.mark("炸了");
        return result;
    }

    //收藏时,保存日报和user关系
    @PostMapping("{newsId}/collections/{userId}")
    public void insertUserAndCollectionRelation(@PathVariable Long newsId,@PathVariable Long userId){
        newsService.insertUserAndCollectionRelation(newsId,userId);
    }

    //更新收藏次数
    @PutMapping({"{id}/collectionNum"})
    public JsonResult updateCollectionNumById(@PathVariable Long id, Integer collectionNum) {
        newsService.updateCollectionNumById(id, collectionNum);
        return new JsonResult();
    }


    //取消收藏时,删除日报和user关系
    @DeleteMapping("{newsId}/collections/{userId}")
    public void deleteCollectionAndUserRelation(@PathVariable Long newsId,@PathVariable Long userId){
        newsService.deleteCollectionAndUserRelation(newsId,userId);
    }
    //获取我的收藏日报
    @GetMapping("/collections")
    public PageInfo queryMyNews(NewsQueryObject qo){
        return newsService.query(qo);
    }

}
